import type { ReactElement } from 'react'
import {
  render,
  unmount as reactUnmount,
} from './render'

/**
 * @description - 将元素渲染到 Document.body
 * @param {ReactElement} element -要渲染的元素
 * @returns {Function} - 卸载已渲染的元素
 */
export function renderToBody(element: ReactElement): () => void {
  const container = document.createElement('div')
  document.body.appendChild(container)
  function unmount() {
    const unmountResult = reactUnmount(container)
    if (unmountResult && container.parentNode) {
      container.parentNode.removeChild(container)
    }
  }
  render(element, container)
  return unmount
}
